package com.konne.nightmare.FastPublicOpinion.ui.information.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.coorchice.library.SuperTextView;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpFragment;
import com.konne.nightmare.FastPublicOpinion.mvp.presenter.CGMSystemPersenter;
import com.konne.nightmare.FastPublicOpinion.mvp.view.ICGMSystemView;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.CGroupManagerDialog;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.GroupManagerActivity;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;

import java.util.List;

import butterknife.BindView;

/**
 * Create by ws.
 * on Data:2022/1/25  Time:下午 5:07.
 * description: ** .
 */
public class CGMSystemFragment extends BaseMvpFragment<ICGMSystemView, CGMSystemPersenter> implements ICGMSystemView, View.OnClickListener {
    @BindView(R.id.ll_group)
    ViewGroup ll_group;
    public static CGMSystemFragment getInstance(){
        return new CGMSystemFragment();
    }
    @Override
    protected CGMSystemPersenter createPresenter() {
        return new CGMSystemPersenter();
    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return null;
    }

    @Override
    public void cancelLoadDialog() {

    }

    @Override
    public int getLayout() {
        return R.layout.frag_cgm_system;
    }

    @Override
    public void initView() {
        findViewById(R.id.stv_add_group).setOnClickListener(this);
        findViewById(R.id.stv_manager).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.stv_add_group:
                mPresenter.addGroup();
                break;
            case R.id.stv_manager:
                new CGroupManagerDialog(getContext()).setcGroupManagerListener(new CGroupManagerDialog.CGroupManagerListener() {
                    @Override
                    public void fun(int type) {
                        if(type==0){
                            mPresenter.renameGroupName();
                        }else {
                            mPresenter.delGroup();
                        }
                    }
                }).show();
                break;
        }
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mPresenter.init();
    }

    @Override
    public void addGroup(String txt) {
        ll_group.addView(getItemGroup(txt));
    }

    @Override
    public void updataGroup(List<String> groups) {
        ll_group.removeAllViews();
        for (int i = 0; i <groups.size() ; i++) {
            ll_group.addView(getItemGroup(groups.get(i)));
        }
    }
    /**
     * 创建小组
     * @param txt 这个为小组数据
     * */
    private View getItemGroup(String txt){
        LayoutInflater inflater=LayoutInflater.from(getContext());
        View view=inflater.inflate(R.layout.item_cgm_group,null);
        SuperTextView stv_sub_title=view.findViewById(R.id.stv_sub_title);
        stv_sub_title.setDrawableRotate(0);
        TextView tv_title=view.findViewById(R.id.tv_title);
        tv_title.setText(txt);
        view.setTag(txt);
        view.setOnClickListener(itemClickListener);
        return view;
    }
    private View.OnClickListener itemClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String txt= (String) view.getTag();
            GroupManagerActivity.start(2);
        }
    };
}
